/**
 * 个人中心
 * @author  Jello
 * @create  2018/11/05 09:10
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var userimg = "";
var array = new Array();
var spinner = new Spinner(opts);

$(function() {
	token = sessionStorage.getItem("user_token") == null ? "" : sessionStorage.getItem("user_token");

	//获取个人资料
	var param = {};
	param.token = token;
	getUserMaterial(param, function(data) {
		var user_name = data.user_name;
		var work_years = data.work_years;
		var birthday = data.birthday;
		var sex = data.user_sex;
		var marry = data.marry_status //情感状态 0保密1未婚 2已婚 3单身 
		var money = data.ideal_money;
		var post = data.ideal_post;
		var work = data.work_type;
		userimg = data.user_url;

		if(sex == 0 || sex == 1) { //男
			$("#man").css("border", "1px solid #3CAFFF");
		} else {
			$("#woman").css("border", "1px solid #3CAFFF");
		}
		var temp = "";
		if(marry == 0) {
			temp = "保密";
		} else if(marry == 1) {
			temp = "未婚";
		} else if(marry == 2) {
			temp = "已婚";
		} else {
			temp = "单身";
		}
		$("#status").val(temp);
		$("#workyear").val(work_years == 6 ? "5年以上" : work_years + "年");
		$("#money").val(money == "10+k" ? "10k以上" : money);
		$("#post").val(post);
		$("#work").val(work);
		$("#userName").html(user_name);
		$("#nickname").val(user_name);
		$("#birthday").val(birthday);
	});

	//查询理想岗位及曾任职工作
	var param = {};
	param.token = token;
	queryMechanismList(param, function(mechanismList) {
		var obj = new Array();
		for(var i = 0; i < mechanismList.length; i++) {
			var html = '<div class="item" value="' + mechanismList[i].id + '" name="' + mechanismList[i].mechanismTypeName + '"><div class="product"><span>' + mechanismList[i].mechanismTypeName + '</span></div></div>';
			$(".all-goods").append(html);
			$(".post-goods").append(html);
			var workTypeList = mechanismList[i].workTypeList;
			for(var j = 0; j < workTypeList.length; j++) {
				array.push(workTypeList[j].mechanismTypeId + "-" + workTypeList[j].workTypeName);
			}
		}
	})

	$('.all-goods .item').hover(function() {
		$(this).addClass('active')
		var goodId = $(this).attr("value");
		var mechanismName = $(this).attr("name");
		var html = "";
		$.each(array, function(index, item) {
			if(goodId == item.split("-")[0]) {
				html += '<span onclick="hideLevelTab(\'' + mechanismName + '\',\'' + item.split("-")[1] + '\')">' + item.split("-")[1] + '</span>';
			}
		});
		var _html = '<div class="product-wrap pos01"><div class="cf">' + html + '</div></div>';
		$(".item").append(_html);
		$(this).find('.product-wrap').show();
	}, function() {
		$(this).removeClass('active');
		$(this).find('.product-wrap').hide();
	});

	$(".all-goods").hover(function() {}, function() {
		$(".all-goods").hide()
	})

	$('.post-goods .item').hover(function() {
		$(this).addClass('active')
		var goodId = $(this).attr("value");
		var mechanismName = $(this).attr("name");
		var html = "";
		$.each(array, function(index, item) {
			if(goodId == item.split("-")[0]) {
				html += '<span onclick="hideLevelTab2(\'' + mechanismName + '\',\'' + item.split("-")[1] + '\')">' + item.split("-")[1] + '</span>';
			}
		});
		var _html = '<div class="product-wrap pos01"><div class="cd">' + html + '</div></div>';
		$(".item").append(_html);
		$(this).find('.product-wrap').show();
	}, function() {
		$(this).removeClass('active');
		$(this).find('.product-wrap').hide();
	});

	$(".post-goods").hover(function() {}, function() {
		$(".post-goods").hide()
	})

	//情感状态
	$("#status").click(function() {
		$("#ul_status").toggle();
	})
	$("#ul_status li").click(function() {
		var value = $(this).text();
		$("#status").val(value);
		$("#ul_status").hide();
	})

	//工作年限
	$("#workyear").click(function() {
		$("#ul_workyear").toggle();
	})
	$("#ul_workyear li").click(function() {
		var value = $(this).text();
		$("#workyear").val(value);
		$("#ul_workyear").hide();
	})

	//期望薪资
	$("#money").click(function() {
		$("#ul_money").toggle();
	})
	$("#ul_money li").click(function() {
		var value = $(this).text();
		$("#money").val(value);
		$("#ul_money").hide();
	})

	//曾任职工作
	$("#work").click(function() {
		$(".all-goods").toggle();
	})

	//理想岗位
	$("#post").click(function() {
		$(".post-goods").toggle();
	})

	//保存
	$("#savebtn").click(function() {
		var nickname = $("#nickname").val();
		var flag = checkNickname(nickname);
		if(flag) {
			var status = $("#status").val();
			if(status == "保密") {
				status = 0;
			} else if(status == "未婚") {
				status = 1;
			} else if(status == "已婚") {
				status = 2;
			} else {
				status = 3;
			}
			var ideaMoney = $("#money").val() == "10k以上" ? "10+k" : $("#money").val();
			var workyear = $("#workyear").val() == "5年以上" ? "6" : $("#workyear").val().replace("年", "");
			var birthday = $("#birthday").val();
			var sex = $("#man").css("border-color") == "rgb(0, 0, 0)" ? "2" : "1";
			var post = $("#post").val();
			var work = $("#work").val();

			$.ajax({
				type: "post",
				url: serverUrl + "/userMaterial/saveOrUpdate?userToken=" + token + "&status=1" + "&userName=" + nickname + "&userSex=" + sex + "&userUrl=" + userimg,
				data: "{\"birthday\":\"" + birthday + "\" , \"ideal_money\":\"" + ideaMoney + "\", \"ideal_post\":\"" + post + "\", \"marry_status\":\"" + status + "\", \"work_type\":\"" + work + "\", \"work_years\":\"" + workyear + "\"}",
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				crossDomain: true,
				timeout: asktime,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					spinner.spin();
					var materialObj = eval(data);
					stopScroll();
					if(materialObj.code == 0 && materialObj.data == 1) {
						$(".errorimg").attr("src", "img/icon/warn.png")
						$(".errortext").html("保存成功");
						$(".errorbox").show();
						setTimeout(function() {
							$(document).unbind("scroll");
							$(".errorbox").hide();
							window.location.reload();
						}, 800);
					} else if(resultObj.code == 10002) {
						window.location.href = "login.html";
					} else {
						$(".errortext").html(data.msg);
						$(".errorbox").show();
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					spinner.spin();
					stopScroll();
					$(".errortext").html("网络异常");
					$(".errorbox").show();
					return;
				}
			});
		}
	})

	$("#nickname").focus(function() {
		$("#error2").html("");
		$("#error1").hide();
	})

	$(".errcover").click(function() {
		$(".errorbox").hide();
		$(document).unbind("scroll");
	})

	$("#man").click(function() {
		$("#man").css("border", "1px solid #3CAFFF");
		$("#woman").css("border", "none");
	})
	$("#woman").click(function() {
		$("#woman").css("border", "1px solid #3CAFFF");
		$("#man").css("border", "none");
	})

	$("#canclebtn").click(function() {
		window.location.reload();
	})

	//菜单栏切换
	$(".info").click(function() {
		$(".tab_nav").toggle();
		$("#menubar").attr("src", $(".tab_nav").is(":hidden") ? "img/personal/arrow_bottom.png" : "img/personal/arrow_top.png");
	});

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword", companyName);
		window.location.href = "applyjob.html";
	})

	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});

	//客服中心
	$("#service_btn1").click(function() {
		window.open("tencent://message/?uin=1423335266&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("tencent://message/?uin=2028048473&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("tencent://message/?uin=3341118684&Site=lanpin123.com&Menu=yes");
	})
})

/**
 * 校验用户昵称
 * @param {Object} nickname
 */
function checkNickname(nickname) {
	if(nickname == "") {
		$("#error1").show();
		$("#error2").html("请输入您的昵称");
		return;
	}
	return true;
}

function hideLevelTab(mechanismType, mechanismName) {
	$(".all-goods").hide();
	$("#work").val(mechanismType + "-" + mechanismName)
}

function hideLevelTab2(mechanismType, mechanismName) {
	$(".post-goods").hide();
	$("#post").val(mechanismType + "-" + mechanismName)
}